// pages/order/settleAccount.js
const {
  weixin
} = require('../../utils/value.js');
const app = require('../../utils/ajax.js');
const config = require('../../utils/config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: -1,
    serviceType: '租床位',
    num: 1,
    checked: false,
    badNumber: '',
    userInfo: {
      name: '---',
      sex: '-',
      telephone: '----',
      ID_card: '------',
      bad_day_money: '0',
      bad_deposit: '0',
      hospital_name: '----',
      list: []
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.handleCheckRegister();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  /**
   * 选择科室
   */
  bindPickerChange: function(e) {
    this.setData({
      index: e.detail.value
    });
  },

  /**
   * 选择
   */
  handleCheck: function() {
    this.setData({
      checked: true
    });
  },

  /**
   * 取消
   */
  handleCancel: function() {
    this.setData({
      checked: false
    });
  },

  /**
   * 跳转到协议页面
   */
  handleRedirectToProtocl: function() {
    wx.navigateTo({
      url: './protocol',
    })
  },

  /**
   * 检查用户注册状态
   */
  handleCheckRegister: function() {
    const url = config.ServerUrl + weixin.SELECT_WEIXIN_USER;
    const param = {
      weixin_id: app.getData('open_id')
    };
    wx.showLoading({
      title: '加载中',
    });
    const _this = this;
    app.doGet(url, param, function(res) {
      if (res.state === 1) {
        _this.handleGetRentInfo();
      } else if (res.state === 5) {
        wx.showToast({
          title: '您还未注册请注册',
          icon: 'none',
          duration: 1000
        })
        setTimeout(function() {
          wx.redirectTo({
            url: '../login/register',
          })
        }, 1000)
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 1000
        })
        setTimeout(function() {
          wx.navigateBack({
            delta: 1
          })
        }, 1500)
      }
    })
  },

  /**
   * 获取租床位的详细信息
   */
  handleGetRentInfo: function() {
    const url = config.ServerUrl + weixin.SELECT_BAD_MSG;
    const param = {
      weixin_id: app.getData('open_id')
    };
    const _this = this;
    app.doGet(url, param, function(res) {
      if (res.state === 1) {
        _this.setData({
          userInfo: res.data
        });
        wx.hideLoading();
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 1000
        })
      }
    })
  },

  /**
   * 处理床位号输入事件
   */
  handleBadNumber: function(e) {
    this.setData({
      badNumber: e.detail.value
    });
  },

  /**
   * 处理支付事件
   */
  handleRecharge: function() {
    const {
      index,
      checked,
      badNumber,
      userInfo
    } = this.data;
    if (index < 0) {
      wx.showToast({
        title: '请选择科室',
        icon: 'none',
        duration: 1000,
      })
      return;
    }
    if (!badNumber || badNumber.length <= 0) {
      wx.showToast({
        title: '请输入床位号',
        icon: 'none',
        duration: 1000,
      })
      return;
    }
    if (!checked) {
      wx.showToast({
        title: '请勾选服务协议',
        icon: 'none',
        duration: 1000,
      })
      return;
    }
    let department_id = userInfo.list[index].department_id;
    let weixin_id=app.getData('open_id');
    let hospital_name=userInfo.hospital_name;
    let money = userInfo.bad_deposit.toString();
    let param={
      weixin_id,
      department_id,
      bed_num: badNumber,
      hospital_name,
      money
    }
    const url = weixin.SUREORDER;
    // wx.showLoading({
    //   title: '支付请求中',
    // })
    app.doPost(url,param,function(res){
      const { timeStamp, nonceStr, signType, paySign}=res;
      console.log(timeStamp,nonceStr,signType,paySign);
      wx.requestPayment({
        'timeStamp':timeStamp.toString(),
        'nonceStr':nonceStr,
        'signType':signType,
        'package':res.package,
        'paySign':paySign,
        success:function(res){
          console.log(res);
          wx.switchTab({
            url: '../order/order',
          })
        },
        fail:function(res){
          console.log(res);
        },
        complete:function(){
          wx.hideLoading();
        }
      });
    })
  }
})